package com.wenwu.controller.other;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.wenwu.common.bean.RelicsAttachment;
import com.wenwu.common.bean.RelicsEntity;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PageUtils;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.common.utils.JsonUtils;
import com.wenwu.common.utils.Status.AttachmentFrom;
import com.wenwu.service.CommonService;
import com.wenwu.service.OtherEntityService;

/**
 * 其他实物资料表
 * 基本信息功能
 * @author Yaphet
 *
 */
@Controller(value = "OtherEntityController")
@RequestMapping(value = "/mgr/otherentity")
public class OtherEntityController {

	Logger log = Logger.getLogger(this.getClass());
	
	private JsonGenerator jsonGenerator;
	
	@Autowired
	private OtherEntityService otherEntityService;
	@Autowired
	private CommonService commonService;
	
	/**
	 * 初始化界面
	 * @return
	 */
	@RequestMapping(value = "/init")
	public String initUserPage() {
		log.info("进入基本信息管理页面...");
		return "otherentity/init";
	}
	
	/**
	 * 进入新增表单页面
	 * @return
	 */
	@RequestMapping(value = "/addForm")
	public String dictionaryform(Model model,
			@RequestParam(required = false, value = "entityId") Integer entityId) {
		log.info("进入基本信息表单页面...");
		try {
			if (entityId != null) {
				List<RelicsAttachment> attachments;
				attachments = commonService.selectAttachmentListByKey(
							entityId, AttachmentFrom.RELICS_ENTITY_01);
				model.addAttribute("attachments", attachments);
			}
		} catch (RelicsException e) {
			log.info(e.getMessage(),e);
		}
		return "otherentity/otherentityform";
	}
	
	/**
	 * 添加
	 */
	@RequestMapping(value = "/saveaddForm")
	public void addBasic(RelicsEntity relicsEntity,HttpServletResponse response) throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		log.info("添加/修改数据：");
		try {
			jsonGenerator.writeObject(otherEntityService.addObj(relicsEntity));
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject(e.getMessage());
		}
	}
	
	/**
	 * 可移动文物登记表表单查询
	 * @return
	 */
	@RequestMapping(value = "/showList")
	public void showList(RelicsEntity relicsEntity,Model model, HttpServletRequest request,
			HttpServletResponse response) {
		log.info("查询可移动文物登记表表单..." );
		PaginationVO pageVo = PageUtils.getPaginationInfo(request);
		try {
			if(relicsEntity!=null && relicsEntity.getmRelicsNo() != null){
				relicsEntity.setmRelicsNo(relicsEntity.getmRelicsNo().trim());
			}
			if(relicsEntity!=null && relicsEntity.getName() != null){
				relicsEntity.setName(relicsEntity.getName().trim());
			}
			
			pageVo = otherEntityService.selectByCondition(relicsEntity, pageVo);
			jsonGenerator = JsonUtils.getJsonGenerator(response);
			jsonGenerator.writeObject(pageVo);// 将PaginationVO对象以json格式写回页面
		} catch (Exception e) {// 异常处理
			log.info(e.getMessage(),e);
			model.addAttribute("errorMsg", "系统异常，请稍后重试!");
		}
	}
	
	/**
	 * 删除信息
	 * 
	 * @param response
	 * @param request
	 * @param key
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteInfo")
	public void deleteInfo(HttpServletResponse response,
			HttpServletRequest request,
			@RequestParam(required = false, value = "keys") String keys)
			throws Exception {
		jsonGenerator = JsonUtils.getJsonGenerator(response);
		try {
			String result = otherEntityService.deleteBykeys(keys);
			jsonGenerator.writeObject(result);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			jsonGenerator.writeObject("删除数据失败");
		}
	}
	
	/**
	 * 显示要修改的信息
	 * 
	 * @param response
	 * @param userId
	 * @throws Exception
	 */
	@RequestMapping(value = "/showUpdate")
	public void showUpdate(HttpServletResponse response,
			@RequestParam(value = "entityId") Integer entityId){
		if (entityId != null) {
			try{
				RelicsEntity RelicsEntity = otherEntityService.selectBySelftKey(entityId);
				jsonGenerator = JsonUtils.getJsonGenerator(response);
				jsonGenerator.writeObject(RelicsEntity);
			}catch (Exception e){
				log.info(e.getMessage(),e);
			}
		}
	}

}
